package com.ruoyi.web.controller.leave;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;
import javax.servlet.http.HttpServletResponse;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.info.domain.InfoClass;
import com.ruoyi.info.domain.InfoStu;
import com.ruoyi.info.domain.InfoTeacher;
import com.ruoyi.info.mapper.InfoClassMapper;
import com.ruoyi.info.service.IInfoClassService;
import com.ruoyi.info.service.IInfoStuService;
import com.ruoyi.info.service.IInfoTeacherService;
import com.ruoyi.leave.domain.vo.LeaveCount;
import com.ruoyi.leave.service.IHolidayService;
import com.ruoyi.leave.service.IParentKnowService;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.leave.domain.LeaveApply;
import com.ruoyi.leave.service.ILeaveApplyService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 请假申请Controller
 * 
 * @author ruoyi
 * @date 2024-09-24
 */
@RestController
@RequestMapping("/leave/apply")
public class LeaveApplyController extends BaseController
{
    @Autowired
    private ILeaveApplyService leaveApplyService;

    @Autowired
    private IInfoStuService infoStuService;

    @Autowired
    private IInfoClassService infoClassService;

    @Autowired
    private IInfoTeacherService infoTeacherService;

    @Autowired
    private IParentKnowService parentKnowService;

    @Autowired
    private IHolidayService holidayService;

    @PreAuthorize("@ss.hasPermi('leave:apply:exportCount')")
    @PostMapping("/exportCount")
    public void exportCount(HttpServletResponse response, LeaveApply leaveApply)
    {
        List<LeaveCount> list = leaveApplyService.listCount(leaveApply);
        ExcelUtil<LeaveCount> util = new ExcelUtil<>(LeaveCount.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:listFdyCount')")
    @GetMapping("/listFdyCount")
    public TableDataInfo listFdyCount(LeaveApply param){
        param.fdyNo = getUsername();
        startPage();
        List<LeaveCount> list = leaveApplyService.listCount(param);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:exportOwnParentKnow')")
    @PostMapping("/exportOwnParentKnow/{id}")
    public void exportOwnParentKnow(@PathVariable("id") Long id,HttpServletResponse response){
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        if(!Objects.equals(stuNo, getUsername())){
            throw new RuntimeException("只能导出自己的家长须知");
        }
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        parentKnowService.getParentKnowWord(parentKnowService.getParentKnow(leaveApply),leaveApply,response);
    }


    @PreAuthorize("@ss.hasPermi('leave:apply:exportParentKnow')")
    @PostMapping("/exportParentKnow/{id}")
    public void exportParentKnow(@PathVariable("id") Long id,HttpServletResponse response){
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        parentKnowService.getParentKnowWord(parentKnowService.getParentKnow(leaveApply),leaveApply,response);
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:exportOwnHoliday')")
    @PostMapping("/exportOwnHoliday/{id}")
    public void exportOwnHoliday(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        if(!Objects.equals(stuNo, getUsername())){
            throw new RuntimeException("只能导出自己的家长须知");
        }
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        QueryWrapper<InfoClass> classQuery = new QueryWrapper<>();
        classQuery.eq("class_code", infoStu.getClassCode());
        InfoClass infoClass = infoClassService.getOne(classQuery);

        if(infoClass != null){
            leaveApply.className = infoClass.getClassName();
        }

        QueryWrapper<InfoTeacher> teacherQuery = new QueryWrapper<>();
        teacherQuery.eq("teacher_no", infoClass.getTeacherNo());
        InfoTeacher infoTeacher = infoTeacherService.getOne(teacherQuery);
        if(infoTeacher != null){
            leaveApply.teacherName = infoTeacher.getTeacherName();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        holidayService.getHolidayWord(holidayService.getHoliday(leaveApply),leaveApply,response);
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:exportHoliday')")
    @PostMapping("/exportHoliday/{id}")
    public void exportHoliday(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        QueryWrapper<InfoClass> classQuery = new QueryWrapper<>();
        classQuery.eq("class_code", infoStu.getClassCode());
        InfoClass infoClass = infoClassService.getOne(classQuery);

        if(infoClass != null){
            leaveApply.className = infoClass.getClassName();
        }

        QueryWrapper<InfoTeacher> teacherQuery = new QueryWrapper<>();
        teacherQuery.eq("teacher_no", infoClass.getTeacherNo());
        InfoTeacher infoTeacher = infoTeacherService.getOne(teacherQuery);
        if(infoTeacher != null){
            leaveApply.teacherName = infoTeacher.getTeacherName();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        holidayService.getHolidayWord(holidayService.getHoliday(leaveApply),leaveApply,response);
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:exportOwnCourse')")
    @PostMapping("/exportOwnCourse/{id}")
    public void exportOwnCourse(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        if(!Objects.equals(stuNo, getUsername())){
            throw new RuntimeException("只能导出自己的家长须知");
        }
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        QueryWrapper<InfoClass> classQuery = new QueryWrapper<>();
        classQuery.eq("class_code", infoStu.getClassCode());
        InfoClass infoClass = infoClassService.getOne(classQuery);

        if(infoClass != null){
            leaveApply.className = infoClass.getClassName();
        }

        QueryWrapper<InfoTeacher> teacherQuery = new QueryWrapper<>();
        teacherQuery.eq("teacher_no", infoClass.getTeacherNo());
        InfoTeacher infoTeacher = infoTeacherService.getOne(teacherQuery);
        if(infoTeacher != null){
            leaveApply.teacherName = infoTeacher.getTeacherName();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        holidayService.getCourseWord(holidayService.getCourse(leaveApply),leaveApply,response);
    }

    @PreAuthorize("@ss.hasPermi('leave:apply:exportCourse')")
    @PostMapping("/exportCourse/{id}")
    public void exportCourse(@PathVariable("id") Long id,HttpServletResponse response) throws IOException {
        LeaveApply leaveApply = leaveApplyService.getById(id);
        if(leaveApply == null){
            throw new RuntimeException("请假申请不存在");
        }
        String stuNo = leaveApply.getStuNo();
        QueryWrapper<InfoStu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("stu_no", stuNo);
        InfoStu infoStu = infoStuService.getOne(queryWrapper);
        if(infoStu == null){
            throw new RuntimeException("学生信息不存在");
        }
        leaveApply.setStuName(infoStu.getStuName());
        QueryWrapper<InfoClass> classQuery = new QueryWrapper<>();
        classQuery.eq("class_code", infoStu.getClassCode());
        InfoClass infoClass = infoClassService.getOne(classQuery);

        if(infoClass != null){
            leaveApply.className = infoClass.getClassName();
        }

        QueryWrapper<InfoTeacher> teacherQuery = new QueryWrapper<>();
        teacherQuery.eq("teacher_no", infoClass.getTeacherNo());
        InfoTeacher infoTeacher = infoTeacherService.getOne(teacherQuery);
        if(infoTeacher != null){
            leaveApply.teacherName = infoTeacher.getTeacherName();
        }

        SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日HH时");
        leaveApply.startTimeStr = sdf.format(leaveApply.startTime);
        leaveApply.endTimeStr = sdf.format(leaveApply.endTime);
        holidayService.getCourseWord(holidayService.getCourse(leaveApply),leaveApply,response);
    }


    @ApiOperation("列出请假申请课程列表")
    @PreAuthorize("@ss.hasPermi('leave:apply:listCourse')")
    @GetMapping("/listCourse/{applyId}")
    public AjaxResult listCourse(@PathVariable("applyId") Long applyId){
         return leaveApplyService.listCourse(applyId);
    }


    @ApiOperation("统计辅导员未审核")
    @PreAuthorize("@ss.hasPermi('leave:apply:countFdyUnAudit')")
    @GetMapping("/countFdyUnAudit")
    public AjaxResult countFdyUnAudit()
    {
        return leaveApplyService.countFdyUnAudit();
    }

    @ApiOperation("审核请假申请")
    @PreAuthorize("@ss.hasPermi('leave:apply:doAudit')")
    @PostMapping("/doAudit")
    public AjaxResult doAudit(@RequestBody LeaveApply param){
        return leaveApplyService.doAudit(param);
    }


    @ApiOperation("辅导员查看请假记录")
    @PreAuthorize("@ss.hasPermi('leave:apply:listFdyAll')")
    @GetMapping("/listFdyAll")
    public TableDataInfo listFdyAll(LeaveApply param){
        param.teacherNo = getUsername();
        startPage();
        List<LeaveApply> list = leaveApplyService.listView(param);
        return getDataTable(list);
    }

    @ApiOperation("辅导员查看请假申请列表")
    @PreAuthorize("@ss.hasPermi('leave:apply:listFdy')")
    @GetMapping("/listFdy")
    public TableDataInfo listFdy(LeaveApply param){
        param.teacherNo = getUsername();
        param.applyStatus = "1";
        startPage();
        List<LeaveApply> list = leaveApplyService.listView(param);
        return getDataTable(list);
    }


    @ApiOperation("学生取消请假")
    @PreAuthorize("@ss.hasPermi('leave:apply:cancelApply')")
    @PostMapping("/cancelApply/{id}")
    public AjaxResult cancelApply(@PathVariable("id") Long id){
        return leaveApplyService.cancelApply(id);
    }


    @ApiOperation("学生查看请假详情")
    @PreAuthorize("@ss.hasPermi('leave:apply:getOwnApply')")
    @GetMapping("/getOwnApply/{id}")
    public AjaxResult getOwnApply(@PathVariable("id") Long id){
        return leaveApplyService.getOwnApply(id);
    }


    @ApiOperation("查询个人请假申请列表")
    @PreAuthorize("@ss.hasPermi('leave:apply:listOwnApply')")
    @GetMapping("/listOwnApply")
    public TableDataInfo listOwnApply(LeaveApply param){
        param.stuNo = getUsername();
        startPage();
        List<LeaveApply> list = leaveApplyService.listView(param);
        return getDataTable(list);
    }



    @PreAuthorize("@ss.hasPermi('leave:apply:doApply')")
    @PostMapping("/doApply")
    public AjaxResult doApply(@Validated @RequestBody LeaveApply param){
        return leaveApplyService.doApply(param);
    }


    /**
     * 查询请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:list')")
    @GetMapping("/list")
    public TableDataInfo list(LeaveApply leaveApply)
    {
        startPage();
        List<LeaveApply> list = leaveApplyService.queryList(leaveApply);
        return getDataTable(list);
    }

    /**
     * 导出请假申请列表
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:export')")
    @Log(title = "请假申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, LeaveApply leaveApply)
    {
        List<LeaveApply> list = leaveApplyService.queryList(leaveApply);
        ExcelUtil<LeaveApply> util = new ExcelUtil<LeaveApply>(LeaveApply.class);
        util.exportExcel(response, list, "请假申请数据");
    }

    /**
     * 获取请假申请详细信息
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(leaveApplyService.getById(id));
    }

    /**
     * 新增请假申请
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:add')")
    @Log(title = "请假申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody LeaveApply leaveApply)
    {
        return toAjax(leaveApplyService.save(leaveApply));
    }

    /**
     * 修改请假申请
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:edit')")
    @Log(title = "请假申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody LeaveApply leaveApply)
    {
        return toAjax(leaveApplyService.updateById(leaveApply));
    }

    /**
     * 删除请假申请
     */
    @PreAuthorize("@ss.hasPermi('leave:apply:remove')")
    @Log(title = "请假申请", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(leaveApplyService.removeByIds(Arrays.asList(ids)));
    }
}
